﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using System.IO;

public partial class Admin_ucControl_ucNewChildMenu : System.Web.UI.UserControl
{
    TblChildMenuDAO menuDAO = new TblChildMenuDAO(Global.CONNECTION_STRING);
    TblParentMenuDAO parentDAO = new TblParentMenuDAO(Global.CONNECTION_STRING);
    TblSupplierDAO supplierDAO = new TblSupplierDAO(Global.CONNECTION_STRING);


    protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        if (!IsPostBack)
        {
            if (Session[Global.SESSION_ADMIN_USER] == "" || Session[Global.SESSION_ADMIN_USER] == null)
            {
                Response.Redirect("~/Admin/Login.aspx");
            }
            if (id == null || id == "")
            {
                LoadChildMenu();
                LoadMenuParent();
                LoadSuppliers();

            }
            else
            {   
                LoadSuppliers();
                LoadMenuParent();
                LoadDetailMenuChild();
                LoadChildMenu();
                
            }

            drncc.Enabled = false;
            uploadFile.Enabled = false;
        }
    }

    private void LoadSuppliers()
    {
        try
        {
            drncc.DataSource = supplierDAO.GetAll();
            drncc.DataTextField = "Suppliers";
            drncc.DataValueField = "IDSupplie";
            drncc.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }

    private void LoadMenuParent()
    {
        try
        {
            drMenuParent.DataSource = parentDAO.GetMenuParent();
            drMenuParent.DataTextField = "MenuParentName";
            drMenuParent.DataValueField = "IDMenu_Parent";
            drMenuParent.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }
    private void LoadDetailMenuChild()
    {
        string id = Request.QueryString["id"];
        DataTable dt =  menuDAO.GetMenuChildByIDChild(Global.ConvertToInt(id));
        foreach (DataRow dr in dt.Rows)
        {
            txtDescription.Content = dr["MenuChilDescription"].ToString();
            txttenungdung.Text = dr["MenuChilName"].ToString();

            string idSupp = dr["IDSupplie"].ToString();
            if (idSupp == null)
            {
                LoadSuppliers();
            }
            else
            {
                drncc.SelectedValue = Global.ConvertToInt(idSupp).ToString();
            }

            string idParentMenu = dr["IDMenu_Parent"].ToString();
            if (idParentMenu == null)
            {
                LoadMenuParent();
            }
            else
            {
                drMenuParent.SelectedValue = Global.ConvertToInt(idParentMenu).ToString();
            }

        }
        
    }

    private void LoadChildMenu()
    {
        try
        {
            grdMenuChild.DataSource = menuDAO.GetMenuChild();
            grdMenuChild.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            string id = Request.QueryString["id"];

            TblChildMenu MenuChild = new TblChildMenu();

            MenuChild.MenuChilDescription = Server.HtmlDecode(txtDescription.Content);
            MenuChild.MenuChilName = txttenungdung.Text;
            MenuChild.IDMenu_Parent = Global.ConvertToInt(drMenuParent.SelectedValue.ToString());

            if (cbchon.Checked == true)
            {              
                MenuChild.IDSupplie = Global.ConvertToInt(drncc.SelectedValue.ToString());
            }
            else
            {
                MenuChild.IDSupplie = 0;
            }

            if (cbupfile.Checked == true)
            {
                string path1 = Server.MapPath("~\\Admin\\images\\Items\\");
                string filename1 = uploadFile.FileName;
                if (File.Exists(path1 + "\\" + filename1))
                {
                    lbMessage.Text = "File này đã tồn tại.Hãy chọn một file khác";
                    return;
                }
                uploadFile.SaveAs(path1 + "\\" + filename1);
                MenuChild.File = filename1;
            }
            else
            {
                MenuChild.File = "";
            }

            if (id == null || id == "")
            {
                
                MenuChild.IDMenuChild = 0;
                menuDAO.InUpChildMenu(MenuChild, 1);
            }
            else
            {
                
                MenuChild.IDMenuChild = Global.ConvertToInt(id);  
                menuDAO.InUpChildMenu(MenuChild, 2);
            }

            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
        {
            LoadChildMenu();
        }
    }
  

   
    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = true;
        try
        {
            TblChildMenu ChildMenu = new TblChildMenu();

            ChildMenu.IDMenuChild = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int idXoa = Convert.ToInt32(((ImageButton)sender).CommandArgument);

            ChildMenu = menuDAO.GetMenuChildByID(idXoa);         
            menuDAO.DeleteChildMenu(ChildMenu);

            flag = true;

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }

        if (flag)
        {
            LoadChildMenu();
        }
    }
    protected void cbchon_CheckedChanged(object sender, EventArgs e)
    {
        if (cbchon.Checked == true)
        {
            drncc.Enabled = true;
        }
        else
        {
            drncc.Enabled = false;
        }
    }
    protected void cbupfile_CheckedChanged(object sender, EventArgs e)
    {
        if (cbupfile.Checked == true)
        {
            uploadFile.Enabled = true;
        }
        else
        {
            uploadFile.Enabled = false;
        }
    }
}
